Thread: modify char array in function || reading lines from file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    87

    modify char array in function || reading lines from file

    I read in file with four columns skipping line one. File is tab/space delimited, my goal is to get the last column.

    I use function where parameter is char array. The function is OK as long it is working, however, it doesn't move the pointer of Char array to new location.

    I cannot understand why. Or, how to move char pointer to location. It prints complete line as 1 startcoord, end coord and gene.

    In the function, the gene name is printed fine, that is pointer is moved correctly.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <string.h>
    
    
    #include "funcs_combinations.h"
    
    
    #define MAX_LEN 200
    
    
    void get_gene_name(char temp_line[])
    {
        int space_count = 0; //get location of last space
        size_t itr = 0; //iterate
        size_t len_line = strlen(temp_line);
    
    
        while (itr < len_line)
        {
            if ((temp_line)[itr] == ' ' || (temp_line)[itr] == '\t')
            {
                space_count = itr; //store space location
            }
            itr++;
        }
        printf("space location was %d\n", space_count);
        temp_line = space_count + temp_line; //move pointer 
        printf("moved location is %d\n", temp_line);
        printf("location is %s\n", temp_line); //printing works fine here but goes bad in returning
        // return (temp_line);
    }
    //////////////////////////////
    
    
    void remove_trailingspaces(char *newlines)
    {
    
    
    /*
    This function is called while reading the first line
    
    
    */
        size_t length = strlen(newlines);
        size_t i = 0;
        for (i = 0; i < length; i++)
        {
            if (newlines[i] == '\n')
            {
                newlines[i - 1] = '\0';
                break;
            }
        }
    }
    
    
    
    
    //////////////////////////////
    int main(int argc, char *argv[])
    {
        FILE *fptr;
        char c[MAX_LEN];
        int line_number = 0;
    
    
        fptr = fopen(argv[1], "r");
    
    
        if (fptr == NULL)
        {
            printf("null pointer for file opening\n");
        }
    
    
        while (fgets(c, MAX_LEN, fptr))
        {
            if (line_number > 1) //make sure header is read 
            {
                if (strcmp(c, "\n") != 0)
                {
                    remove_trailingspaces(c);
                    get_gene_name(c);
                    printf("new gene is %s\n", c); // it doesn't move to new location 
                    add_node(&start, c); //only if not new line
                }
            }
    
    
            line_number++;
        }
    
    
        fclose(fptr);
    
    
        return 0;
    }
    I have input file with lines as:

    CHR START END GENE
    1 1234 546 GENE1
    1 2234 5346 GENE12
    1 4234 5246 GENE14
    1 6234 5546 GENE16
    I want to get last column, that is gene name. GENE1, GENE12 and such from function get_gene_name.
    Edit: itr for get_gene_name (while loop )
    Last edited by deathmetal; 03-07-2021 at 07:29 PM. Reason: incorrect iteration

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2013, 12:24 PM
  2. Trying to modify array in a shared library function
    By JulietBoy in forum C Programming
    Replies: 8
    Last Post: 05-24-2013, 05:39 AM
  3. Replies: 2
    Last Post: 05-04-2013, 04:29 PM
  4. modify function pass it array
    By a.mlw.walker in forum C Programming
    Replies: 12
    Last Post: 08-01-2011, 04:03 AM
  5. Reading lines from a File
    By DivineSlayer936 in forum C++ Programming
    Replies: 12
    Last Post: 04-02-2007, 12:12 PM

Tags for this Thread